package com.home.dao;

import java.util.List;

import com.home.po.Light;
import com.home.util.DB;
import com.home.util.Tool;

public class LightDao {
	private DB db = DB.getInstance();

	public void insert(Light light) {
		String sql = "insert into light (name,no,state,time,auto) values (?,?,?,?,?)";
		Object[] params = {light.getName(),light.getNo(),light.getState(),light.getTime(),light.getAuto()};
		db.executeUpdate(sql, params);
	}
	
	public void delete(Integer id) {
		String sql = "delete from light where l_id = ?";
		db.executeUpdate(sql, new Object[]{id});
	}
	
	public List<Light> select(Light light, int start, int pageSize) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select * from light where 1=1 ");
		if(light.getName() != null) {
			buffer.append(" and name='" + light.getName() + "'");
		}
		if(light.getNo() != null) {
			buffer.append(" and no="+light.getNo());
		}
		if(light.getState() != null) {
			buffer.append(" and state=" + light.getState());
		}
		if(light.getTime() != null) {
			buffer.append(" and time='" + Tool.dateToString(light.getTime()) + "'");
		}
		if(light.getAuto() != null) {
			buffer.append(" and auto=" + light.getAuto());
		}
		buffer.append(" limit " + start + ", " + pageSize);
		String sql = buffer.toString();
		List<Light> lightList = db.executeQuery(sql, null);
		return lightList;
	}
	
	public void update(Light light) {
		String sql = "update light set name=?, no=?, state=?, auto=?, time=? where l_id=?";
		Object[] params = {light.getName(),light.getNo(),light.getState(),light.getAuto(),light.getTime(),light.getlId()};
		db.executeUpdate(sql, params);
	}
	
}
